﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using SharpMap.Presentation.Wpf.StyleView;
using System.Windows.Data;

namespace SharpMap.Presentation.Wpf
{
    public class StyleListItem : ContentControl
    {
        static StyleListItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(StyleListItem), 
                new FrameworkPropertyMetadata( typeof(StyleListItem)));
        }

        public StyleListItem()
        {
            SetOwningPropertyContainer(this, this);
            Binding.AddSourceUpdatedHandler(this, SourceUpdatedHandler);
        }

        void SourceUpdatedHandler(object target, DataTransferEventArgs e)
        {
            LayerCommands.StyleChanged.Execute(StyleViewEntry, this);
        }

        public StyleViewEntry StyleViewEntry
        {
            get { return (StyleViewEntry)GetValue(StyleViewEntryProperty); }
            set { SetValue(StyleViewEntryProperty, value); }
        }


        public static readonly DependencyProperty StyleViewEntryProperty =
            DependencyProperty.Register("StyleViewEntry", typeof(StyleViewEntry), typeof(StyleListItem),
            new FrameworkPropertyMetadata(default(StyleViewEntry)));

            public static StyleListItem GetOwningPropertyContainer(DependencyObject obj)
          {
              return (StyleListItem)obj.GetValue(OwningPropertyContainerProperty);
          }

          public static void SetOwningPropertyContainer(DependencyObject obj, StyleListItem value)
          {
              obj.SetValue(OwningPropertyContainerProperty, value);
          }

          public DataTemplateSelector EditorTemplateSelector
          {
              get { return _itemTemplateSelector; }
          }
          public static readonly DependencyProperty OwningPropertyContainerProperty =
              DependencyProperty.RegisterAttached("OwningPropertyContainer", typeof(StyleListItem), typeof(StyleListItem), 
              new FrameworkPropertyMetadata(default(StyleListItem)
                  ,FrameworkPropertyMetadataOptions.Inherits));

          static StyleItemEditorTemplateSelector _itemTemplateSelector = new StyleItemEditorTemplateSelector();
    }
}
